import key from 'keymaster';
import {Effect} from "dva";
import { Reducer } from 'redux';

export interface StateType {
  record?: 0;
  current?: 0;
}

export interface IndexModelType {
  namespace: string;
  state: StateType;
  effects: {
    minus1: Effect;
  };
  reducers: {
    add: Reducer<StateType>;
    minus: Reducer<StateType>;
  };
}



const model: IndexModelType ={

  namespace: 'count',
  state: {
    record: 0,
    current: 0,
  },

  effects: {
    *minus1(action, { call, put }) {
      yield put({ type: 'minus' });
    },
  },

  reducers: {
    add(state) {
      const newCurrent = state.current + 1;
      return { ...state,
        record: newCurrent > state.record ? newCurrent : state.record,
        current: newCurrent,
      };
    },
    minus(state) {
      return { ...state, current: state.current - 1 };
    },
  },

  // subscriptions: {
  //   keyboardWatcher({ dispatch }) {
  //     key('ctrl+up', () => { dispatch({type:'add'}) });
  //   },
  // },

};

export default model;

